#!/bin/sh -x
# get tivodecode, mplayer, libfaac, libx264
#
# git clone git://git.videolan.org/x264.git
# 
# sudo ln -s /Developer/SDKs/MacOSX10.4u.sdk/usr/lib/crt1.o /Developer/SDKs/MacOSX10.4u.sdk/usr/lib/crt1.10.5.o

SRCDIR=$HOME/src
LAME=lame
BOOST=boost_1_38_0
FAAD=faad2-2.7
FAAC=faac-1.28
FFMPEG_LIBS="libavutil libavformat libavcodec libavdevice libswscale"

  cd $SRCDIR
  mkdir binaries

  export CFLAGS="-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch i386 -arch ppc7400"
  export CPPFLAGS=""
  export LDFLAGS="-arch i386 -arch ppc7400 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk"

  cd $SRCDIR/$BOOST
  ./tools/jam/src/bin.macosxx86/bjam  --user-config=user-config-yoav.jam --with-system --with-filesystem architecture=combined link=static

  cd $SRCDIR/t2sami-2.0.0033-pre2
  ./configure --without-boost  --disable-dependency-tracking
  make BOOST_CPPFLAGS="-I../$BOOST/" BOOST_LDFLAGS="-L../$BOOST/" BOOST_FILESYSTEM_LIB="../$BOOST/bin.v2/libs/filesystem/build/darwin-4.0.1/release/architecture-combined/link-static/threading-multi/libboost_filesystem-xgcc40-mt-1_38.a ../$BOOST/bin.v2/libs/system/build/darwin-4.0.1/release/architecture-combined/link-static/threading-multi/libboost_system-xgcc40-mt-1_38.a"
  cp t2sami ../binaries/

  cd $SRCDIR/streambaby
  find . -name \*.java | xargs -n 1 $SRCDIR/remove_override.pl
  ant clean
  ant
  if [ "$?" -ne "0" ]; then
      echo "Failed to build streambaby"
      exit 1
  fi
  cd dist/
  rm *-src.zip
  rm -rf streambaby ../../binaries/streambaby
  unzip streambaby*.zip
  rm streambaby*.zip
  mv streambaby* ../../binaries/streambaby
  cd ..
  svn info . | grep Revision | cut -d ' ' -f 2 > ../binaries/streambaby/VERSION.txt

  cd $SRCDIR/etv-comskip/src/comskip
  make clean; rm -f ../../comskip
  make ../../comskip CFLAGS="$CFLAGS -O2 -fasm-blocks -Wno-unused  -DHAVE_CONFIG_H -force_cpusubtype_ALL -I. -Ilibmpeg2 -IAC3Dec -Iargtable2-7/src" LFLAGS="$LDFLAGS -headerpad_max_install_names -prebind -force_cpusubtype_ALL"
  if [ "$?" -ne "0" ]; then
      echo "Failed to build comskip"
      exit 1
  fi
  cp ../../comskip ../../../binaries/comskip

  cd $SRCDIR/atomicparsley
  autoconf && autoheader
  export CXXFLAGS="-isysroot /Developer/SDKs/MacOSX10.4u.sdk"
  export LDFLAGS="-Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk"
  make clean
  ./configure --enable-universal && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build AtomicParsley"
      exit 1
  fi
  unset CXXFLAGS
  cp AtomicParsley ../binaries/

   export CFLAGS='-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch i386'
   export CXXFLAGS='-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch i386'
   export CPPFLAGS='-arch i386'
   export LDFLAGS='-arch i386 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk'
  cd $SRCDIR/x264
  make clean
  ./configure && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build i386 x264"
      exit 1
  fi

  cd $SRCDIR/$FAAC
  make clean
  ./configure --without-mp4v2 --enable-shared=no --enable-static=yes && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build i386 faac"
      exit 1
  fi

  cd $SRCDIR/$FAAD
  make clean
  ./configure --enable-shared=no --enable-static=yes && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build i386 faad"
      exit 1
  fi

  cd $SRCDIR/$LAME
  make clean
  ./configure  --disable-shared --disable-frontend && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build i386 lame-mp3"
      exit 1
  fi

  cd $SRCDIR/tivodecode-0.2pre4
  make distclean
  ./configure --disable-dependency-tracking && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build tivodecode"
      exit 1
  fi
  cp tivodecode ../binaries/tivodecode-i386

  export CFLAGS='-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch i386 -O2 -fomit-frame-pointer'
  export CXXFLAGS='-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch i386 -O2 -fomit-frame-pointer'
  export CPPFLAGS='-arch i386'
  export LDFLAGS='-arch i386 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk'
  cd $SRCDIR/mplayer
  make distclean
  rm ./uname
  ./configure --disable-png --enable-mp3lame --enable-x264 --disable-mplayer --enable-faac --enable-faad-internal --disable-freetype --extra-cflags="-I$SRCDIR/$FAAC/include/ -I$SRCDIR/x264 -I$SRCDIR/$LAME/include" --extra-ldflags="-L$SRCDIR/$FAAC/libfaac/.libs/ -L$SRCDIR/x264 -L$SRCDIR/$LAME/libmp3lame/.libs" --extra-libs-mencoder="-lx264 $SRCDIR/$FAAC/libfaac/.libs/libfaac.a" && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build i386 mencoder"
      exit 1
  fi
  cp ./mencoder ../binaries/mencoder-i386

   export CFLAGS='-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch i386 -O2 -fomit-frame-pointer'
   export CXXFLAGS='-isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch i386 -O2 -fomit-frame-pointer'
   export CPPFLAGS='-arch i386'
   export LDFLAGS='-arch i386 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk/usr/lib'
   export CFLAGS='-arch i386'
   export CXXFLAGS='-arch i386'
   export CPPFLAGS='-arch i386'
   export LDFLAGS='-arch i386'
  cd $SRCDIR/ffmpeg
  make distclean
  ./configure --shlibdir="@executable_path" --arch=i386 --cpu=i686 --disable-static --enable-shared --enable-pthreads --enable-gpl --enable-libmp3lame --enable-libfaac --enable-nonfree --enable-libfaad --enable-libx264 --extra-cflags="-I$SRCDIR/$LAME/include -I$SRCDIR/$FAAD/include -I$SRCDIR/$FAAC/include -I$SRCDIR/x264" --extra-ldflags="-L$SRCDIR/$LAME/libmp3lame/.libs -L$SRCDIR/$FAAD/libfaad/.libs -L$SRCDIR/$FAAC/libfaac/.libs -L$SRCDIR/x264/" && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build i386 ffmpeg"
      exit 1
  fi
  for libname in $FFMPEG_LIBS; do
    cp ./$libname/$libname.dylib ../binaries/$libname.dylib.i386
    install_name_tool -change /usr/local/lib/$libname.dylib @executable_path/$libname.dylib ./ffmpeg
  done
  cp ./ffmpeg ../binaries/ffmpeg-i386

  if [ x"$ONLYX86" = x"yes" ] ; then
     exit
  fi

   export CFLAGS='-arch ppc7400 -isysroot /Developer/SDKs/MacOSX10.4u.sdk'
   export CXXFLAGS='-arch ppc7400 -isysroot /Developer/SDKs/MacOSX10.4u.sdk'
   export CPPFLAGS='-arch ppc7400 -isysroot /Developer/SDKs/MacOSX10.4u.sdk'
   export LDFLAGS='-arch ppc7400 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk'
 
   cd $SRCDIR/x264
   make distclean
   ./configure --host=powerpc-apple-darwin && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build ppc x264"
      exit 1
  fi

  cd $SRCDIR/$FAAC
  make distclean
   ./configure --host=ppc-apple-darwin --without-mp4v2 --enable-shared=no --enable-static=yes && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build ppc faac"
      exit 1
  fi

  cd $SRCDIR/$FAAD
  make clean
  ./configure --host=ppc-apple-darwin --enable-shared=no --enable-static=yes && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build ppc faad2"
      exit 1
  fi

  cd $SRCDIR/$LAME
  make distclean
  ./configure --host=ppc-apple-darwin --disable-shared --disable-frontend && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build ppc lame-mp3"
      exit 1
  fi

  cd $SRCDIR/tivodecode-0.2pre4
  make distclean
  ./configure --disable-dependency-tracking && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build tivodecode"
      exit 1
  fi
  cp tivodecode ../binaries/tivodecode-ppc

  cd $SRCDIR/mplayer
  make distclean
  export OPATH=$PATH
  export PATH=.:$PATH
  echo '#!/usr/bin/perl
$arg=$ARGV[0];
if ($arg eq "-p") {
    print "powerpc\n";
} elsif ($arg eq "-m") {
    print "powerpc\n";
}else {
    print `/usr/bin/uname $arg`;
}
' > uname
  chmod a+rx uname
  ./configure --disable-png --enable-mp3lame --enable-x264 --disable-mplayer --enable-faac --enable-faad-internal --disable-freetype --extra-cflags="-I$SRCDIR/$FAAC/include/ -I$SRCDIR/x264 -I$SRCDIR/$LAME/include" --extra-ldflags="-L$SRCDIR/$FAAC/libfaac/.libs/ -L$SRCDIR/x264 -L$SRCDIR/$LAME/libmp3lame/.libs" --extra-libs-mencoder="-lx264 $SRCDIR/$FAAC/libfaac/.libs/libfaac.a" && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build ppc mencoder"
      exit 1
  fi
  export PATH=$OPATH
  rm uname
  cp ./mencoder ../binaries/mencoder-ppc

   export CFLAGS='-arch ppc7400 -isysroot /Developer/SDKs/MacOSX10.4u.sdk'
   export CXXFLAGS='-arch ppc7400 -isysroot /Developer/SDKs/MacOSX10.4u.sdk'
   export CPPFLAGS='-arch ppc7400 -isysroot /Developer/SDKs/MacOSX10.4u.sdk'
   export LDFLAGS='-arch ppc7400 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk/usr/lib'
   export CFLAGS='-arch ppc7400'
   export CXXFLAGS='-arch ppc7400'
   export CPPFLAGS='-arch ppc7400'
   export LDFLAGS='-arch ppc7400'
 
   cd $SRCDIR/ffmpeg
   make distclean
   ./configure --shlibdir="@executable_path" --arch=ppc --cpu=G4 --enable-shared --disable-static --enable-pthreads --disable-mmx --enable-gpl --enable-libmp3lame --enable-libfaac  --enable-nonfree --enable-libfaad --enable-libx264 --extra-cflags="-DHAVE_LLRINT -I$SRCDIR/$LAME/include -I$SRCDIR/$FAAD/include -I$SRCDIR/$FAAC/include -I$SRCDIR/x264" --extra-ldflags="-L$SRCDIR/$LAME/libmp3lame/.libs -L$SRCDIR/$FAAD/libfaad/.libs -L$SRCDIR/$FAAC/libfaac/.libs -L$SRCDIR/x264/" && make
  if [ "$?" -ne "0" ]; then
      echo "Failed to build ppc ffmpeg shared libraries"
      exit 1
  fi
  for libname in $FFMPEG_LIBS; do
    cp ./$libname/$libname.dylib ../binaries/$libname.dylib.ppc
    install_name_tool -change /usr/local/lib/$libname.dylib @executable_path/$libname.dylib ./ffmpeg
  done
  cp ./ffmpeg ../binaries/ffmpeg-ppc

  cd $SRCDIR/binaries
  rm -rf "../pytivox/streambaby"
  cp -R streambaby "../pytivox/"
  lipo -create tivodecode-i386 tivodecode-ppc -output tivodecode
  rm -f tivodecode-*
  lipo -create mencoder-i386 mencoder-ppc -output mencoder
  rm -f mencoder-*
  lipo -create ffmpeg-i386 ffmpeg-ppc -output ffmpeg
  rm -f ffmpeg-*
  for x in $FFMPEG_LIBS;  do  echo $x 
     lipo -create $x.dylib.ppc $x.dylib.i386 -output $x.dylib
  done
  cp t2sami comskip mencoder tivodecode AtomicParsley "../itivo/External Software/"
  (cd "../itivo/External Software" && strip *)
  cp ffmpeg "../pytivox/External Software/ffmpeg.bin"
  cp *.dylib "../pytivox/External Software/"
  (cd "../pytivox/External Software/" && strip ffmpeg.bin)
  (cd "../pytivox/pyTivo-wmcbrine/" && git log --summary | head -10 | grep Date | cut -d ' ' -f 5-6,8 > VERSION.txt)
